package com.cy.ppj.web.admin.mall;

import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.mall.GoodsAddAO;
import com.cy.ppj.model.ao.mall.GoodsEditAO;
import com.cy.ppj.model.domian.mall.Goods;
import com.cy.ppj.model.enums.CommonStatus;
import com.cy.ppj.model.query.mall.GoodsAdminListQuery;
import com.cy.ppj.model.vo.mall.GoodsAdminDetailVO;
import com.cy.ppj.model.vo.mall.GoodsAdminListVO;
import com.cy.ppj.service.comm.FileService;
import com.cy.ppj.service.mall.GoodsService;
import com.cy.ppj.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @Description 后台-商品管理
 * @Author tanyilin
 * @Date 2019/7/3 18:17
 */
@RestController
@RequestMapping("/admin/mall/goods")
@Api(tags = "后台-商品管理")
public class AdminGoodsController extends BaseController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private FileService  fileService;

    @ApiOperation("商品列表")
    @GetMapping("/list")
    public PageBean<GoodsAdminListVO> getList(GoodsAdminListQuery query) {

        PageBean<GoodsAdminListVO> pageBean = goodsService.getGoodsAdminList(query);
        return pageBean;
    }

    @ApiOperation("查看详情")
    @GetMapping("/detail")
    public Result<GoodsAdminDetailVO> getDetail(@RequestParam(value = "goodsId") Long goodsId) {

        GoodsAdminDetailVO detailVO = goodsService.getGoodsAdminDetail(goodsId);
        return ok("查看商品详情成功", detailVO);
    }

    @ApiOperation("上下架状态")
    @PostMapping("/update/status")
    public Result updateGoodsStatus(@RequestParam(value = "goodsId") Long goodsId, @RequestParam(value = "status") CommonStatus status) {

        Goods updateGoods = new Goods();
        updateGoods.setId(goodsId);
        updateGoods.setStatus(status);
        goodsService.updateGoods(updateGoods);

        return ok("更改商品状态成功");
    }

    @ApiOperation("新增商品")
    @PostMapping("/do/add")
    public Result doAdd(@RequestBody @Validated GoodsAddAO addAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        goodsService.addGoods(addAO);

        return ok("新增商品成功");
    }

    @ApiOperation("编辑商品")
    @PostMapping("/do/edit")
    public Result doEdit(@RequestBody @Validated GoodsEditAO editAO, BindingResult bindingResult) {

        this.checkError(bindingResult);

        goodsService.editGoods(editAO);

        return ok("编辑商品成功");
    }

    @ApiOperation("上传商品图片")
    @PostMapping("/upload/image")
    public Result uploadImage(@RequestParam(value = "file") MultipartFile file) {

        String directory = "goods/";
        Result<Map<String, String>> uploadResult = fileService.uploadToMallBucket(file, directory, null);
        return uploadResult;
    }

}
